package org.xbs.demo1.controller;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xbs.demo1.pojo.Dept;
import org.xbs.demo1.result.Result;
import org.xbs.demo1.service.IDeptService;

@Slf4j
@RestController
@RequestMapping("/depts")
@CrossOrigin//括号内输入对应的前端地址
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @GetMapping//访问这个url获得所有部门的数据
    public Result getDepts(){
        //日志记录
        log.info("查询所有部门数据");
        //调用service中getDepts()方法
        return Result.success(deptService.getDepts());
    }
    @DeleteMapping//传入数据为id，通过url
    public Result deleteDeptsById(@RequestParam("id") Integer id){
        //日志记录
        log.info("根据id删除部门数据：{}",id);
        //调用service中deleteDeptsById()方法
        deptService.deleteDeptsById(id);
        return Result.success();
    }
    @PutMapping//传入一个dept对象，通过请求体
    public Result addDepts(@RequestBody Dept dept){
        //日志记录
        log.info("增加部门数据：{}",dept);
        //调用service中addDepts()方法
        deptService.addDepts(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getDeptsById(@PathVariable("id") Integer id){
        //日志记录
        log.info("根据id查询回显：{}",id);
        //调用service中getDeptsById()方法
        return Result.success(deptService.getDeptsById(id));
    }
    @PostMapping//传入参数为请求体，方法为post
    public Result updateDepts(@RequestBody Dept dept){
        //日志记录
        log.info("更新部门数据：{}",dept);
        //调用service中updateDepts()方法
        deptService.updateDepts(dept);
        return Result.success();
    }
}
